(function() {

YAHOO.namespace ("drafTrack"); 

var Dom = YAHOO.util.Dom; var Event = YAHOO.util.Event; var DDM = YAHOO.util.DragDropMgr; 

YAHOO.drafTrack.DDList = function(id, node, sGroup, config) {    YAHOO.drafTrack.DDList.superclass.constructor.call(this, id, sGroup, config);
    this.resizeFrame = false;    this.logger = this.logger || YAHOO;
    this.node = node;
    Dom.addClass(this.getDragEl(),'dragEl');};YAHOO.extend(YAHOO.drafTrack.DDList, YAHOO.util.DDProxy, {
	node: null,
	destNode: null,
	previousDestNode: null,
	dropMode: null, //3 drop mode available:
					//forbidden: if the node can t be drop on this location
					//leaf if the destNode is a leaf, the element is added after
					//child if the destNode is not a leaf, the node is dropped at the end of the children list
	childLeafTriggerPos: null, //Contains the Yposition in pixel after which the child/leaf is trigerred
	childLeafTrigger:0.5, //number between 0 and 1 where the behaviour of the DD elem between leaf and child should be triggered			
	    startDrag: function(x,y) {
    	
    	
    	//We start by deactivating the cell editor is present
    	this.node.tree.cancelCellEditor();
    	        this.logger.log(this.id + " startDrag");		this.logger.log(this.node);        // make the proxy look like the source element        var dragEl = this.getDragEl();        var clickEl = this.getEl();        dragEl.innerHTML = this.node.data[this.node.tree.columns[0].dataIndex];
        dragEl.style.width = "300px";    },    endDrag: function(e) {
    	
    	this.logger.log(this.id + " endDrag");        // Show the proxy element and animate it to the src element's location        Dom.setStyle(this.getDragEl(), "visibility", "hidden");        if(!this.destNode) return;    },    onDragDrop: function(e, id) {
    
		if(!this.destNode) return;
        if(this.dropMode == 'forbidden') return;        // remove        thisParent = this.node.parent;        this.node.tree.popNode(this.node);      	
      	thisParent.refresh();
      	

              // fixes bug where a parent with 1 item is still marked        // expanded, so when a new node is dragged to it, it doesn't        // draw the new node (thinks it's already expanded)        if (thisParent.children.length == 0) {          thisParent.expanded = false;        }
        
        if(this.dropMode == 'leaf')
        {
            YAHOO.log('leaf Mode, inserting node '+ this.node.index +' after '+this.destNode.index);
        	this.node.insertAfter(this.destNode);
        	
        	this.destNode.parent.refresh();
        	
        	DDM.refreshCache();
        	
        	this.previousDestNode = this.destNode;
            this.destNode = false;
            this.setStyle();
            this.previousDestNode = false;
             
        	return;
        }
        else if(this.dropMode == 'child')
        {
          YAHOO.log('child Mode, inserting node '+ this.node.index +' as a child of '+this.destNode.index);
          this.node.appendTo(this.destNode);
          
          this.destNode.parent.refresh();

          
          DDM.refreshCache();
          
          this.previousDestNode = this.destNode;
          this.destNode = false;
          this.setStyle();
          this.previousDestNode = false;
          
      	  return;
        }    },    onDrag: function(e) {    },	onDragEnter: function(e,id) {
	    this.previousDestNode = this.destNode;
		this.calculateDropMode(id);
		this.setStyle();
	},
	onDragOut: function(e,id) {
	},
	    onDragOver: function(e, id) {

    if(!this.destNode) return;
    if(!this.destNode.hasChildren(true)) return;
    if(this.dropMode == 'forbidden') return;
    var y = Event.getPageY(e);
    
    YAHOO.log('y: ' +y +'  triggerPos: '+this.childLeafTriggerPos);
    
    if(y > this.childLeafTriggerPos) 
    {
    	YAHOO.log('leaf mode triggered');
    	this.dropMode = 'leaf';
    }
    else  this.dropMode = 'child';
    
    this.previousDestNode = this.destNode;
    this.setStyle();    },
    
    setStyle: function() {
    	if(this.previousDestNode) 
		{
			Dom.removeClass(this.previousDestNode.getDDId(), "dropModeForbidden");
			Dom.removeClass(this.previousDestNode.getDDId(), "dropModeLeaf");
			Dom.removeClass(this.previousDestNode.getDDId(), "dropModeChild");
			Dom.removeClass(this.getDragEl(),"dragElDropModeForbidden");
			Dom.removeClass(this.getDragEl(),"dragElDropModeLeaf");
			Dom.removeClass(this.getDragEl(),"dragElDropModeChild");
		}
		if(this.destNode) 
		{
			if(this.dropMode == 'forbidden') 
			{
			Dom.addClass(this.destNode.getDDId(), "dropModeForbidden");
			Dom.addClass(this.getDragEl(),"dragElDropModeForbidden");
			}
			if(this.dropMode == 'leaf') 
			{
			Dom.addClass(this.destNode.getDDId(), "dropModeLeaf");
			Dom.addClass(this.getDragEl(),"dragElDropModeLeaf");
			}
			if(this.dropMode == 'child') 
			{
				Dom.addClass(this.destNode.getDDId(), "dropModeChild");
				Dom.addClass(this.getDragEl(),"dragElDropModeChild");
			}
		}
    },
    
    	calculateDropMode: function(id) {
		var srcEl = this.getEl();        var destEl = Dom.get(id);        
        // We are only concerned with list items, we ignore any other drop triggers
        
       		//We retrieve the id of the DestNode        	var match = /ygtvdd(\d+)/.exec(destEl.id);
                	var destNode = this.node.tree.getNodeByIndex(match[1]);		
			//First mode forbidden if the destNode is descendant of the node
			if(destNode.isDescendentOf(this.node)) 
			{
				this.dropMode = 'forbidden'; 
				this.destNode = destNode;
				YAHOO.log('dropMode : forbidden');
				return;
			}
			else if(destNode.isLeaf)
			{
				this.dropMode = 'leaf'; 
				this.destNode = destNode;
				YAHOO.log('dropMode : leaf, destNode : ' + destNode.index );
				return;
			}
			else
			{
				this.dropMode = 'child'; 
				this.destNode = destNode;
				
				//Calculation of the trigger for the behaviour between leave and child
				var pos = YAHOO.util.Dom.getRegion('ygtvcontentel' + this.destNode.index + 'task');
				this.childLeafTriggerPos =  pos.top + parseInt((pos.bottom-pos.top)*this.childLeafTrigger);

				YAHOO.log('dropMode : child, destNode : ' + destNode.index );
				return;
			}
		
	}
    });

})();